<template>
  <view class="content">
    <u-navbar :placeholder='true' title="地址管理" leftIcon="arrow-leftward"
              @leftClick="leftClick('/pages/mine/setting/index')">
    </u-navbar>
    <view class="table" v-for="(item,index) in addresslist " :key="index">
      <view class="top">
        <text class="top-l" v-if="item.status==0">默認</text>
        <view class="top-r">
          {{ item.address }}
        </view>
      </view>
      <view class="center">
        <view class="center-l">
          {{ item.address }}
        </view>
        <image @click="toedit(item)" class="center-r" src="@/static/images/adress_edit.png" mode=""></image>
      </view>
      <view class="bottom">
        <view style="margin-right: 20rpx;">
          {{ item.consignee }}
        </view>
        <view class="">
          {{ item.mobile }}
        </view>
      </view>
    </view>
    <view class="" style="height: 200rpx;">

    </view>

    <view class="btn" @click="address">
      添加新地址
    </view>
  </view>
</template>

<script>
import {
  getaddress,
  addAddress
} from '@/network/api/setting.js'

export default {
  data() {
    return {
      data: {
        search: '',
        sortAttr: '',
        sortMode: '',
        limit: '',
        page: 1,
        last_page: 0,
      },
      addresslist: [],
    }
  },
  onShow() {
    this.data.page = 1
    this.addresslist = []
    this.getAddress()
  },
  onReachBottom() {
    if(this.data.page<this.last_page){
      this.data.page++
      this.getAddress()
    }
  },
  methods: {
    async getAddress() {
      //加载中
      uni.showLoading({
        title: '加载中'
      })
      const res = await getaddress(this.data)
      uni.hideLoading()
      // console.log(res);
      let newaddresslist = res.data.address
      this.addresslist = [...this.addresslist, ...newaddresslist]
      this.last_page = res.data.page.last_page
    },
    leftClick(url) {
      uni.navigateTo({
        url: url
      })
    },
    toedit(item) {
      uni.navigateTo({
        url: `/pages/mine/setting/address/edit?address_id=${item.address_id}`
      })
    },
    address() {
      uni.navigateTo({
        url: `/pages/mine/setting/address/edit`
      })
    }
  }
}
</script>

<style lang="scss">
.content {
  position: relative;

  .table {
    margin: 32rpx;
    padding-bottom: 32rpx;
    border-bottom: 1rpx solid #DEDEDE;

    .top {
      display: flex;

      .top-l {
        padding: 0 12rpx;
        font-size: 20rpx;
        background: #F5F6F9;
        border-radius: 8rpx;
        margin-right: 5rpx;
      }

      .top-r {
        font-size: 24rpx;
        font-family: PingFang SC;
        font-weight: 500;
        color: #999999;
      }
    }

    .center {
      padding: 12rpx 0;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .center-l {
        font-size: 28rpx;
        font-family: PingFang SC;
        font-weight: bold;
        color: #333333;
      }

      .center-r {
        width: 40rpx;
        height: 40rpx;
      }
    }

    .bottom {
      display: flex;
      font-size: 24rpx;
      font-family: PingFang SC;
      font-weight: 500;
      color: #999999;
    }
  }

  .btn {
    position: fixed;
    bottom: 15rpx;
    left: 50%;
    transform: translateX(-50%);
    font-size: 32rpx;
    font-family: PingFang SC;
    font-weight: bold;
    line-height: 80rpx;
    color: #FFFFFF;
    width: 686rpx;
    height: 80rpx;
    text-align: center;
    background: #645AED;
    opacity: 1;
    border-radius: 30rpx;
  }
}
</style>
